package com.example.sxy_smartpark

import android.os.Bundle
import android.os.CountDownTimer
import android.view.View
import com.alibaba.android.arouter.launcher.ARouter
import com.example.lib_base.path.RouterPath
import com.example.lib_common.view.adapter.ImageBannerAdapter
import com.example.lib_common.view.entity.BannerEntity

import com.example.lib_common.view.view.BaseMvvmActivity
import com.example.lib_image.glide.glide.GlideImgLoader
import com.example.sxy_smartpark.databinding.ActivitySplashBinding
import com.tencent.mmkv.MMKV
import com.youth.banner.indicator.CircleIndicator
import com.youth.banner.listener.OnPageChangeListener
import com.youth.banner.transformer.AlphaPageTransformer
import dagger.hilt.android.AndroidEntryPoint

/**
 * 业务：1.倒计时：2.APP主图Banner切换：3.广告
 */
@AndroidEntryPoint
class SplashActivity : BaseMvvmActivity<ActivitySplashBinding>() {
    private var bannerData = mutableListOf<BannerEntity>()
    private lateinit var mBannerAdapter: ImageBannerAdapter
    private lateinit var mCountDownTimer: CountDownTimer



    override fun getLayoutId(): Int = R.layout.activity_splash

    override fun initViews() {
        val isFisrt = MMKV.defaultMMKV().decodeBool("isFirstBoot", true)
        if (isFisrt){
            MMKV.defaultMMKV().encode("isFirstBoot", false)
            showAppBanner()
        }else{
            showAppAdv()
        }
    }

    override fun getActivityState(state: Bundle) {

    }

    override fun saveActivityState(outState: Bundle) {

    }

    /**
     * 展示APP主图
     */
    private fun showAppBanner(){
        //View.VISIBLE表示显示UI，View.INVISIBLE表示不显示UI，但是可以占据UI大小和位置，View.GONE表示既不显示UI也不占据大小和位置
        mDataBinding.clAppBanner.visibility = View.VISIBLE
        mDataBinding.clAppAdv.visibility = View.GONE

        bannerData.run {
            add(BannerEntity(0, R.drawable.img_1, ""))
            add(BannerEntity(1, R.drawable.img_2, ""))
            add(BannerEntity(2, R.drawable.img_3, ""))
        }

        mBannerAdapter = ImageBannerAdapter(bannerData)

        mDataBinding.appBanner.run {
            setAdapter(mBannerAdapter)
            //关联生命周期
            addBannerLifecycleObserver(this@SplashActivity)
            //设置指示器
            indicator = CircleIndicator(this@SplashActivity)
            //设置切换效果
            addPageTransformer(AlphaPageTransformer())
            //设置页面切换监听
            addOnPageChangeListener(object: OnPageChangeListener{
                override fun onPageScrolled(position: Int, positionOffset: Float,positionOffsetPixels: Int) {}

                override fun onPageSelected(position: Int) {
                    //判断Banner是否滑动到最后一页
                    if (position == bannerData.size - 1){
                        mDataBinding.tvStart.visibility = View.VISIBLE
                    } else {
                        mDataBinding.tvStart.visibility = View.INVISIBLE
                    }
                }

                override fun onPageScrollStateChanged(state: Int) { }

            })
        }

        //点击立即进入
        mDataBinding.tvStart.setOnClickListener {
            this@SplashActivity.jumpMainPage()
        }
    }

    //展示广告
    private fun showAppAdv(){
        mDataBinding.clAppBanner.visibility = View.GONE
        mDataBinding.clAppAdv.visibility = View.VISIBLE

        //显示广告图片
        mDataBinding.ivAdv.run {
            GlideImgLoader.loadResImage(this,R.drawable.back, this)
        }

        //5秒倒计时
        mCountDownTimer = object : CountDownTimer(5000, 1000){
            override fun onTick(millisUntilFinished: Long) {
                mDataBinding.tvAdvSkip.text = "${millisUntilFinished / 1000}秒跳过"
            }

            override fun onFinish() {
                this@SplashActivity.jumpMainPage()
            }
        }

        //启动倒计时
        mCountDownTimer?.start()
    }

    override fun onResume() {
        super.onResume()

    }

    override fun onDestroy() {
        super.onDestroy()
        mCountDownTimer?.cancel()
    }

    private fun jumpMainPage(){
        ARouter.getInstance().build(RouterPath.PATH_MAIN_PAGE).navigation()
    }


}